package com.hjw.controller.adm;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.hjw.entity.Order;
import com.hjw.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @创建时间：2025-04-25
 * @文件名称：satoken_sobuk - com.hjw.controller.adm
 */
@Slf4j
@RestController
@RequestMapping("/api/adm/order")
public class AdmOrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/findByCondition")
    public SaResult findByCondition(@RequestBody Map<String, Object> map) {
        if (map == null || map.isEmpty()){
            return SaResult.get(203,"查询参数为空", null);
        }
        log.info("查询条件：{}", map);
        if (map.get("payStatus") != null && map.get("payStatus").toString().isEmpty()){
            map.remove("payStatus");
        }
        if (map.get("shipStatus") != null && map.get("shipStatus").toString().isEmpty()){
            map.remove("shipStatus");
        }
        if (map.get("ship2phone") != null && map.get("ship2phone").toString().isEmpty()){
            map.remove("ship2phone");
        }
        if (map.get("ship2name") != null && map.get("ship2name").toString().isEmpty()){
            map.remove("ship2name");
        }
        if (map.get("account") != null && map.get("account").toString().isEmpty()){
            map.remove("account");
        }
        if (map.get("lines") != null && map.get("lines").toString().equals("0")){
            map.remove("lines");
        }else {
            Integer lines = Integer.parseInt(map.get("lines").toString());
            map.put("lines",lines);
        }
        //默认查询语句从第一条开始
        map.put("curPage",0);
        List<Order> orders = orderService.findByCondition(map);
        if (orders == null){
            log.info("查询结果为空");
            return SaResult.get(-200,"查询结果为空", null);
        }
        Map<String,Object> result = new HashMap<>(2);
        result.put("orders",orders);
        result.put("total",orderService.getCountByCondition(map));
        return SaResult.get(200,"查询成功", result);
    }

    @PostMapping("/shipGoods")
    public SaResult updateOrder(@RequestBody Map<String,Object> map){
        log.info("发货请求参数：{}", map);
        if (map == null || map.isEmpty()){
            return SaResult.get(-200,"请求参数为空", null);
        }
        if (map.get("ordid") == null || map.get("ordid").toString().isEmpty()){
            return SaResult.get(-200,"订单信息不得为空", null);
        }
        map.put("shipStatus",1);
        if (map.get("shipno") == null || map.get("shipno").toString().isEmpty()){
            return SaResult.get(-200,"快递单号不得为空", null);
        }
        if (map.get("shipper") == null || map.get("shipper").toString().isEmpty()){
            return SaResult.get(-200,"快递公司名称不得为空", null);
        }

        int update = orderService.update(map);
        if (update == 0){
            return SaResult.get(-200,"更新失败", null);
        }

        Map<String,Object> m = (Map<String, Object>) map.get("find");
        SaResult result = this.findByCondition(m);
        return SaResult.get(200,"更新成功", result.getData());
    }


}
